var XMLstring = "";
var XMLindent = "\t";
var HTMLstring= "";
var im0_name = "book.gif";
var im1_name = "books_open.gif";
var im2_name = "books_close.gif";
function setNewName(){
	var newname = document.getElementById('newname').value;
	tree.setItemText(tree.getSelectedItemId(),newname);
}
function getXMLtree(){
	XMLstring = "<?xml version='1.0'?>\n<tree id=\"0\">\n";
	var allItems = tree.getSubItems(0).toString().split(",");
	for(var i=0; i<allItems.length; i++){
		treeLooper(allItems[i]);
	}
	XMLstring += "</tree>";
	//document.getElementById('tester').innerHTML = XMLstring;
	return XMLstring;
}
function treeLooper(lid){
	var XMLindent_temp = XMLindent;
	if(tree.hasChildren(lid)){
		XMLstring += XMLindent+"<item text=\""+tree.getItemText(lid)+"\" id=\""+lid+"\""+checked(lid)+" href=\""+aInfo[lid]['href']+"\" open=\"1\">\n";
		XMLindent += "\t";
		var Items = tree.getSubItems(lid).toString().split(",");
		for(var i=0; i<Items.length; i++){
			treeLooper(Items[i]);
		}
		XMLstring += XMLindent_temp+"</item>\n";
		XMLindent = XMLindent_temp;
	}else{
		XMLstring += XMLindent_temp+"<item text=\""+tree.getItemText(lid)+"\" id=\""+lid+"\""+checked(lid)+" href=\""+aInfo[lid]['href']+"\" />\n";
	}

}
function getHTMLtree(){
	HTMLstring = "<ul class='menulist' id='listMenuRoot'>\n";
	var allItems = tree.getSubItems(0).toString().split(",");
	for(var i=0; i<allItems.length; i++){
		if(tree.isItemChecked(allItems[i])) treeLooperHTML(allItems[i]);
	}
	HTMLstring += "</ul>";
	//document.getElementById('tester').innerHTML = HTMLstring;
	return HTMLstring;
}
function treeLooperHTML(lid){
	var XMLindent_temp = XMLindent;
	if(!tree.isItemChecked(lid)) return;
	if(tree.hasChildren(lid)){
		HTMLstring += XMLindent+"<li><a href='"+aInfo[lid]['href']+"' id=\"menuitem"+lid+"\">"+tree.getItemText(lid)+"</a>\n";
		XMLindent += "\t";
		HTMLstring += XMLindent+"<ul>\n";
		XMLindent += "\t";
		var Items = tree.getSubItems(lid).toString().split(",");
		for(var i=0; i<Items.length; i++){
			treeLooperHTML(Items[i]);
		}
		HTMLstring += XMLindent_temp+"\t</ul>\n";
		HTMLstring += XMLindent_temp+"</li>\n";
		XMLindent = XMLindent_temp;
	}else{
		HTMLstring += XMLindent_temp+"<li><a href='"+aInfo[lid]['href']+"' id=\"menuitem"+lid+"\">"+tree.getItemText(lid)+"</a></li>\n";
	}

}
function newMenuItem(id){
	aInfo[id] = new Array();
	aInfo[id]['href']='?page='+id;
}
function checked(id){
	var checked = tree.isItemChecked(id);
	if(checked) return " checked=\"1\" ";
	return "";
}
function deleteMenu(){
	if(confirm("are you sure you want to delete this menu?")) tree.deleteItem(tree.getSelectedItemId(), true);
}

function onNodeSelect(id){
	document.getElementById('newname').value = tree.getItemText(id);
	document.getElementById('link').value = aInfo[id]['href'];
	document.getElementById('activeID').innerHTML = id;
}
var aInfo = new Array();
tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
//var tree = dhtmlXTreeFromHTML('tree_source');
tree.setImagePath("./imgs/");
tree.enableDragAndDrop(1);
tree.loadXML("menufiles/structure.xml");
tree.setOnClickHandler(onNodeSelect);
tree.setStdImages("book.gif","books_open.gif","books_close.gif"); 
tree.enableCheckBoxes(1);
